// 撒点地图，平面地图，gis地图公共mixin

import TileLayer from 'ol/layer/Tile';
import TileGrid from 'ol/tilegrid/TileGrid.js';
import XYZ from 'ol/source/XYZ';
import tileConfig from '@/components/common/map-components/tilelayer-select/tile-config.js';
import { colorToRgba } from '@/utils/util.js';

export default {
  emits: ['addReqParamsToXvue'],
  methods: {

    // 获取地图区域名文字位置
    textPoint(feature) {
      let point;
      if (feature.getGeometry().getInteriorPoints) {
        const polygons = feature.getGeometry().getPolygons();
        let polygon;
        let area = 0;
        polygons.forEach(item => {
          const itemArea = item.getArea();
          if (itemArea > area) {
            polygon = item;
            area = itemArea;
          }
        });
        point = polygon.getInteriorPoint();
      } else {
        point = feature.getGeometry().getInteriorPoint();
      }
      return point;
    },

    creatBaseLayer() {
      const { property } = this.config.config;
      if (this.baseLayer) {
        this.map.removeLayer(this.baseLayer);
      }
      if (property.baseMapType === 'noBaselayer' || !this.getXYZUrl()) {
        return;
      }
      const { projection, origin, resolutions } = property;
      if (projection === 'EPSG:4326') {
        if (origin && resolutions) {
          this.baseLayer = new TileLayer({
            source: new XYZ({
              tileGrid: new TileGrid({
                tileSize: 256,
                origin: JSON.parse(origin),
                resolutions: JSON.parse(resolutions),
              }),
              projection,
              url: this.getXYZUrl(),
            }),
          });
          this.map.addLayer(this.baseLayer);
        }
      } else {
        this.baseLayer = new TileLayer({
          source: new XYZ({
            url: this.getXYZUrl(),
          }),
        });
        this.map.addLayer(this.baseLayer);
      }
    },

    getXYZUrl() {
      const { property } = this.config.config;
      if (property.urlOrigin === 'select') {
        const tileItem = tileConfig.filter(item => item.key === property.tileTabActiveName)[0];
        return tileItem.mapList[property.tabMapIndex].url;
      }
      return property.url;
    },

    /*
     * 改变rgb颜色透明度
     * rgba(40, 157, 255, 0.3) => rgba(40, 157, 255, 0.8)
     * rgb(40, 157, 255) => rgba(40, 157, 255, 0.8)
     */
    tranColorOpacity(color, opacity = 1) {
      return colorToRgba(color, opacity);
    },

    _getFeatureParmasXminer(dataSourceType, level) {
      // 区域数据参数
      const pEl = this.params.params.find(item => item.code === 'parent_code');
      const el = this.params.params.find(item => item.code === 'code');
      if (pEl) {
        const index = this.params.params.indexOf(pEl);
        this.params.params.splice(index, 1);
      }
      if (el) {
        const index = this.params.params.indexOf(el);
        this.params.params.splice(index, 1);
      }
      this.params.params.push({
        code: level < this.levelNum ? 'parent_code' : 'code',
        value: this.region,
      });
      if (dataSourceType === 'xMiner') {
        this.$emit('addReqParamsToXvue', this.params);
      } else {
        this.$emit('addReqParamsToXvue', {});
      }
    },

    _getFeatureParmasXtag(level) {
      const { conditions } = this.params;

      // 清空terms里自带的parent_code和code
      let hasTermsValue = false;
      for (const con of conditions) {
        let hasValue = false;
        if (!con.terms) {
          continue;
        }
        const pEl = con.terms.find(item => Boolean(item.parent_code));
        const el = con.terms.find(item => Boolean(item.code));
        if (pEl) {
          const index = con.terms.indexOf(pEl);
          con.terms.splice(index, 1);
          hasValue = true;
          hasTermsValue = true;
        }
        if (el) {
          const index = con.terms.indexOf(el);
          con.terms.splice(index, 1);
          hasValue = true;
          hasTermsValue = true;
        }

        // 如果已有参数，则直接添加新值
        if (hasValue) {
          const key = level < this.levelNum ? 'parent_code' : 'code';
          con.terms.push({
            [key]: [this.region],
          });
        }
      }

      // 如果没有参数，则添加新参数
      if (!hasTermsValue) {
        const key = level < this.levelNum ? 'parent_code' : 'code';
        conditions.push({
          terms: [{ [key]: [this.region] }],
        });
      }
      this.$emit('addReqParamsToXvue', {});
    },

    getFeatureParams(data, level) {
      const { dataSourceType, dataSource } = data;
      if (['xMiner', 'xMinerPublicDatabase', 'fabric', 'Dac'].includes(dataSourceType)) {
        this._getFeatureParmasXminer(dataSourceType, level);
      } else if (dataSourceType === 'xTag') {
        this._getFeatureParmasXtag(level);
      } else {}
      const _params = { ...this.params };

      // 公用数据库增加请求参数
      if (dataSourceType === 'xMinerPublicDatabase') {
        _params.isCommonDatasource = true;
        _params.datasourceCode = dataSource;
      }
      return _params;
    },
  },
};
